package com.yunupmall.controller.amdin;

import com.yunupmall.api.po.MemberSexPo;
import com.yunupmall.api.po.UserCounts;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.entity.admin.memberpojo.MemberSexCounts;
import com.yunupmall.entity.admin.memberpojo.Memberwhere;
import com.yunupmall.service.admin.service.MemberService;
import com.yunupmall.uits.Result;
import com.yunupmall.uits.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CencusController {
    @Autowired
    private  MemberService memberService;
    @Autowired
    private Result result;

    //查询用户性别数据
    @RequestMapping("/getManCouns")
    public Result<List<MemberSexPo>>  getManCouns() {
        try {
            return result.ok(memberService.getmanCounts());
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    //查询所有用户信息
    @GetMapping("/queryAllmemberData")
    public String queryAllmemberData(Integer page, Integer size) {
        int querydatanum = memberService.querydatanum();
        List<Member> memberList = memberService.queryAllMemberdata((page - 1) * size, size);
        String returnData = ReturnData.getReturnData(querydatanum, memberList);
        return returnData;
    }

    //修改用户使用权限
    @PostMapping("/updatememberstatics")
    public String updatememberstatics(Integer statics, Integer id) {
        int i = memberService.updatememberStatics(statics, id);
        if (i > 0) {
            return "ok";
        }
        return "fail";
    }

    //多条件会员信息检索
    @GetMapping("/querywherememberRend")
    public String querywherememberRend(@RequestParam(value = "MemberName", required = false) String MemberName,
                                       @RequestParam(value = "MemberSex", required = false) String memberSex,
                                       @RequestParam("page") Integer page,
                                       @RequestParam("size") Integer size) {
        Memberwhere memberwhere = new Memberwhere(MemberName, memberSex, (page - 1) * size, size);
        List<Member> memberList = memberService.querywhereData(memberwhere);
        int i = memberService.queryCounts();
        String returnData = ReturnData.getReturnData(i, memberList);
        return returnData;
    }
    /**
     * 用户增长数据
     */
    @GetMapping("/getmouthCounts")
    @ResponseBody
    public Result<List<UserCounts>> getCounts(@RequestParam(value="data",required = false) String date){
        try {
            return result.ok(memberService.getCounts(date));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
}
